package utils

import (
	"crypto/rand"
)

// RandomToken returns a random alphanumeric token of given length
func RandomToken(n int) (string, error) {
	const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	b := make([]byte, n)
	if _, err := rand.Read(b); err != nil {
		return "", err
	}
	out := make([]byte, n)
	for i := 0; i < n; i++ {
		out[i] = letters[int(b[i])%len(letters)]
	}
	return string(out), nil
}
